package com.njitt.bysj.config;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.net.CookieManager;
import java.nio.channels.Selector;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: wyj
 * @time: 2022/3/18 23:57
 */
@Configuration
public class ShiroConfig {


    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager manager) {
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        /*添加shiro 的过滤器
        anon :  无需认证就可访问
        authc:必须认证才能访问
        user:必须拥有记住我功能
        role: 拥有某个角色才能访问
        */

        Map<String, String> filterMap = new HashMap<>(16);
        filterMap.put("/index", "anon");
        filterMap.put("/main", "user");
        filterMap.put("/uIndex", "user");
        //授权3
        bean.setLoginUrl("index");
        bean.setFilterChainDefinitionMap(filterMap);
        bean.setSecurityManager(manager);
        return bean;
    }


    @Bean
    public DefaultWebSessionManager getDefaultWebSessionManager() {
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        sessionManager.setGlobalSessionTimeout(5*1000);
        return sessionManager;
    }


    @Bean
    public CookieRememberMeManager cookieRememberMeManager() {
        CookieRememberMeManager rememberMeManager = new CookieRememberMeManager();
        SimpleCookie simpleCookie = new SimpleCookie("rememberMe");
        simpleCookie.setMaxAge(2678400);
        rememberMeManager.setCookie(simpleCookie);
        rememberMeManager.setCipherKey(Base64.decode("6ZmI6I2j3Y+R1aSn5BOlAA=="));
        return rememberMeManager;
    }



    /**
     * @description: 第二部
     * @return: * @param null
     * @author:
     * @time: 2022/3/18 23:59
     */
    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(@Qualifier("userRealm") UserRealm realm) {
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        manager.setSessionManager(getDefaultWebSessionManager());
        manager.setRememberMeManager(cookieRememberMeManager());
        manager.setRealm(realm);

        // 设置remember 记住我
        return manager;
    }

    /**
     * @return : 第一步
     * @author :wyanjia
     * @parm :
     * @date : 2022/3/17 19:33
     */
    @Bean
    public UserRealm userRealm() {
        return new UserRealm();
    }


    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager manager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(manager);
        return authorizationAttributeSourceAdvisor;
    }
}
